﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Indigo.Quibbler.Common
{
    /// <summary>
    /// Interaction logic for ImageButton.xaml
    /// </summary>
    public partial class ImageButton : UserControl
    {
        #region Properties
        public string ImageSource
        {
            get { return (string)GetValue(ImageSourceProperty); }
            set { SetValue(ImageSourceProperty, value); }
        }

        public string HoverImageSource
        {
            get { return (string)GetValue(HoverImageSourceProperty); }
            set { SetValue(HoverImageSourceProperty, value); }
        }



        public bool ToggleStatus
        {
            get { return (bool)GetValue(ToggleStatusProperty); }
            set { SetValue(ToggleStatusProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ToggleStatus.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ToggleStatusProperty =
            DependencyProperty.Register("ToggleStatus", typeof(bool), typeof(ImageButton), new PropertyMetadata(OnToggleStatusChanged));



        #region Dependency Property
        public static readonly DependencyProperty ImageSourceProperty = DependencyProperty.Register("ImageSource", typeof(string), typeof(ImageButton), new PropertyMetadata(null, OnImageSourceChanged));
        public static readonly DependencyProperty HoverImageSourceProperty = DependencyProperty.Register("HoverImageSource", typeof(string), typeof(ImageButton), new PropertyMetadata(null, OnImageSourceChanged));
        #endregion

        #endregion


        #region Public Events
        public event EventHandler Click;
        #endregion

        #region Ctor
        public ImageButton()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(ImageButton_Loaded);
            //imageButton.MouseEnter += new MouseEventHandler(ImageButton_MouseEnter);
            //imageButton.MouseLeave += new MouseEventHandler(ImageButton_MouseLeave);
            imageButton.Click += new RoutedEventHandler(imageButton_Click);
        }




        void ImageButton_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {

                DependencyObject obj = VisualTreeHelper.GetChild(this, 0);
                while ((obj is Grid) == false)
                {
                    obj = VisualTreeHelper.GetChild(obj, 0);
                }
                if (ToggleStatus)
                {
                    Image imageRef = (Image)VisualTreeHelper.GetChild(obj, 0);
                    BitmapDecoder decoder = BitmapDecoder.Create(new Uri(HoverImageSource, UriKind.RelativeOrAbsolute), BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                    ImageSource iconSource = decoder.Frames[0];
                    imageRef.Source = iconSource;

                    imageRef = (Image)VisualTreeHelper.GetChild(obj, 1);
                    decoder = BitmapDecoder.Create(new Uri(HoverImageSource, UriKind.RelativeOrAbsolute), BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                    iconSource = decoder.Frames[0];
                    imageRef.Source = iconSource;
                }
                else
                {
                    Image imageRef = (Image)VisualTreeHelper.GetChild(obj, 0);
                    BitmapDecoder decoder = BitmapDecoder.Create(new Uri(ImageSource, UriKind.RelativeOrAbsolute), BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                    ImageSource iconSource = decoder.Frames[0];
                    imageRef.Source = iconSource;

                    imageRef = (Image)VisualTreeHelper.GetChild(obj, 1);
                    decoder = BitmapDecoder.Create(new Uri(HoverImageSource, UriKind.RelativeOrAbsolute), BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                    iconSource = decoder.Frames[0];
                    imageRef.Source = iconSource;
                }

            }
            catch (Exception ex) { }
        }

        #endregion

        #region Event Handlers

        void imageButton_Click(object sender, RoutedEventArgs e)
        {
            if (Click != null)
            {
                Click(this, e);
            }
        }


        void ImageButton_MouseLeave(object sender, MouseEventArgs e)
        {
            try
            {
                if (!ToggleStatus)
                {
                    DependencyObject obj = VisualTreeHelper.GetChild(this, 0);
                    while ((obj is Grid) == false)
                    {
                        obj = VisualTreeHelper.GetChild(obj, 0);
                    }
                    Image imageRef = (Image)VisualTreeHelper.GetChild(obj, 0);
                    imageRef.Opacity = 1;

                    imageRef = (Image)VisualTreeHelper.GetChild(obj, 1);
                    imageRef.Opacity = 0;
                }
            }
            catch { }
        }

        void ImageButton_MouseEnter(object sender, MouseEventArgs e)
        {
            try
            {
                DependencyObject obj = VisualTreeHelper.GetChild(this, 0);
                while ((obj is Grid) == false)
                {
                    obj = VisualTreeHelper.GetChild(obj, 0);
                }
                Image imageRef = (Image)VisualTreeHelper.GetChild(obj, 0);
                imageRef.Opacity = 0;

                imageRef = (Image)VisualTreeHelper.GetChild(obj, 1);
                imageRef.Opacity = 1;
            }
            catch { }
        }

        #endregion

        private static void OnImageSourceChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            try
            {
                ImageButton imageButton = (ImageButton)sender;
                DependencyObject obj = VisualTreeHelper.GetChild(imageButton, 0);
                while ((obj is Grid) == false)
                {
                    obj = VisualTreeHelper.GetChild(obj, 0);
                }
                //Image imageRef = (Image)VisualTreeHelper.GetChild(obj, 0);
                //BitmapDecoder decoder = BitmapDecoder.Create(new Uri(imageButton.ImageSource, UriKind.RelativeOrAbsolute), BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                //ImageSource iconSource = decoder.Frames[0];
                //imageRef.Source = iconSource;

                //imageRef = (Image)VisualTreeHelper.GetChild(obj, 1);
                //decoder = BitmapDecoder.Create(new Uri(imageButton.HoverImageSource, UriKind.RelativeOrAbsolute), BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                //iconSource = decoder.Frames[0];
                //imageRef.Source = iconSource;
                if (imageButton.ToggleStatus)
                {
                    Image imageRef = (Image)VisualTreeHelper.GetChild(obj, 0);
                    BitmapDecoder decoder = BitmapDecoder.Create(new Uri(imageButton.HoverImageSource, UriKind.RelativeOrAbsolute), BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                    ImageSource iconSource = decoder.Frames[0];
                    imageRef.Source = iconSource;

                    imageRef = (Image)VisualTreeHelper.GetChild(obj, 1);
                    decoder = BitmapDecoder.Create(new Uri(imageButton.HoverImageSource, UriKind.RelativeOrAbsolute), BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                    iconSource = decoder.Frames[0];
                    imageRef.Source = iconSource;
                }
                else
                {
                    Image imageRef = (Image)VisualTreeHelper.GetChild(obj, 0);
                    BitmapDecoder decoder = BitmapDecoder.Create(new Uri(imageButton.ImageSource, UriKind.RelativeOrAbsolute), BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                    ImageSource iconSource = decoder.Frames[0];
                    imageRef.Source = iconSource;

                    imageRef = (Image)VisualTreeHelper.GetChild(obj, 1);
                    decoder = BitmapDecoder.Create(new Uri(imageButton.HoverImageSource, UriKind.RelativeOrAbsolute), BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                    iconSource = decoder.Frames[0];
                    imageRef.Source = iconSource;
                }

            }
            catch (Exception ex) { }
        }

        private static void OnToggleStatusChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            try
            {
                ImageButton imageButton = (ImageButton)sender;
                DependencyObject obj = VisualTreeHelper.GetChild(imageButton, 0);
                while ((obj is Grid) == false)
                {
                    obj = VisualTreeHelper.GetChild(obj, 0);
                }

                if ((bool)e.NewValue)
                {
                    Image imageRef = (Image)VisualTreeHelper.GetChild(obj, 0);
                    BitmapDecoder decoder = BitmapDecoder.Create(new Uri(imageButton.HoverImageSource, UriKind.RelativeOrAbsolute), BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                    ImageSource iconSource = decoder.Frames[0];
                    imageRef.Source = iconSource;

                    imageRef = (Image)VisualTreeHelper.GetChild(obj, 1);
                    decoder = BitmapDecoder.Create(new Uri(imageButton.HoverImageSource, UriKind.RelativeOrAbsolute), BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                    iconSource = decoder.Frames[0];
                    imageRef.Source = iconSource;
                }
                else
                {
                    Image imageRef = (Image)VisualTreeHelper.GetChild(obj, 0);
                    BitmapDecoder decoder = BitmapDecoder.Create(new Uri(imageButton.ImageSource, UriKind.RelativeOrAbsolute), BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                    ImageSource iconSource = decoder.Frames[0];
                    imageRef.Source = iconSource;

                    imageRef = (Image)VisualTreeHelper.GetChild(obj, 1);
                    decoder = BitmapDecoder.Create(new Uri(imageButton.HoverImageSource, UriKind.RelativeOrAbsolute), BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                    iconSource = decoder.Frames[0];
                    imageRef.Source = iconSource;
                }
            }
            catch (Exception ex) { }
        }
    }
}
